export const timeFormat = (date) => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  return `${year}-${month}-${day}`
}

export const getTime = (num) => {
  const start = new Date()
  if (num && typeof num === 'number' && num > 0) {
    start.setTime(start.getTime() - 3600 * 1000 * 24 * num)
    return timeFormat(start)
  } else {
    return timeFormat(start)
  }
}

export const isJSON = (str) => {
  try {
    JSON.parse(str)
    return true
  } catch (e) {
    return false
  }
}

export const downLoad = (res) => {
  const blob = new Blob([res.data], { type: 'application/zip'})
  const a = document.createElement('a')
  const href = window.URL.createObjectURL(blob)
  a.href = href
  const disposition = res.headers['content-disposition']
  const filename = disposition?.split('filename=')[1]?.split('.')[0]
  a.download = filename
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
  window.URL.revokeObjectURL(href)
}

export const downLoadExcel = (res) => {
  const blob = new Blob([res.data], { type: res.headers['content-type']})
  const a = document.createElement('a')
  const href = window.URL.createObjectURL(blob)
  a.href = href
  const disposition = res.headers['content-disposition']
  const filename = disposition?.split("filename*=UTF-8''")[1]?.split('.')[0] || disposition?.split("filename=")[1]?.split('.')[0]
  const name = decodeURI(filename)
  a.download = `${name}.xlsx`
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
  window.URL.revokeObjectURL(href)
}
export const pickerOptionsDateRange = {
  shortcuts: [
    {
      text: '最近一周',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近六个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 182)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一年',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
        picker.$emit('pick', [start, end])
      }
    }
  ]
}
export const pickerOptionsDateRange1 = {
  shortcuts: [
    {
      text: '最近一周',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近六个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 182)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一年',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近三年',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365 * 3)
        picker.$emit('pick', [start, end])
      }
    },
  ]
}
export const rowClassName = ({ row }) => {
  if (row.unSave) {
    return 'unSave_color'
  }
  if (row.is_ignore === 1) {
    return 'is_ignore'
  }
}
export const setMenuSelected = (num) => {
  const ele = document.getElementsByClassName('menu')
  ele[0]?.children[num]?.children[0]?.classList.add("selected")
}
export const cancelMenuSelected = (num) => {
  const ele = document.getElementsByClassName('menu')
  ele[0]?.children[num]?.children[0]?.classList.remove("selected")
}
export const yesOrNo = [
  { label: '是', value: '是' },
  { label: '否', value: '否' }
]